package com.example.myapplication2.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class UserDao {
    private DatabaseHelper dbHelper;

    public UserDao(Context context) {
        dbHelper = new DatabaseHelper(context);
    }

    // 注册用户
    public boolean registerUser(String username, String password) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DatabaseHelper.COLUMN_USERNAME, username);
        values.put(DatabaseHelper.COLUMN_PASSWORD, password);

        try {
            long result = db.insert(DatabaseHelper.TABLE_USERS, null, values);
            return result != -1;
        } catch (Exception e) {
            return false;
        } finally {
            db.close();
        }
    }

    // 验证用户登录
    public boolean validateUser(String username, String password) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String[] columns = {DatabaseHelper.COLUMN_ID};
        String selection = DatabaseHelper.COLUMN_USERNAME + " = ? AND " + 
                         DatabaseHelper.COLUMN_PASSWORD + " = ?";
        String[] selectionArgs = {username, password};

        try {
            Cursor cursor = db.query(DatabaseHelper.TABLE_USERS, columns, selection, 
                                   selectionArgs, null, null, null);
            boolean exists = cursor != null && cursor.getCount() > 0;
            if (cursor != null) {
                cursor.close();
            }
            return exists;
        } finally {
            db.close();
        }
    }

    // 检查用户名是否已存在
    public boolean isUserExists(String username) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String[] columns = {DatabaseHelper.COLUMN_ID};
        String selection = DatabaseHelper.COLUMN_USERNAME + " = ?";
        String[] selectionArgs = {username};

        try {
            Cursor cursor = db.query(DatabaseHelper.TABLE_USERS, columns, selection, 
                                   selectionArgs, null, null, null);
            boolean exists = cursor != null && cursor.getCount() > 0;
            if (cursor != null) {
                cursor.close();
            }
            return exists;
        } finally {
            db.close();
        }
    }

    // 获取用户ID
    public long getUserId(String username) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        long userId = -1;
        
        String[] columns = {DatabaseHelper.COLUMN_ID};
        String selection = DatabaseHelper.COLUMN_USERNAME + " = ?";
        String[] selectionArgs = {username};
        
        try {
            Cursor cursor = db.query(DatabaseHelper.TABLE_USERS, columns, selection, 
                                    selectionArgs, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                userId = cursor.getLong(0);
                cursor.close();
            }
        } finally {
            db.close();
        }
        return userId;
    }
} 